package cn.rtangyy.seckilling.service.impl;

import cn.rtangyy.seckilling.dao.UserDao;
import cn.rtangyy.seckilling.entity.User;
import cn.rtangyy.seckilling.exception.GlobalException;
import cn.rtangyy.seckilling.service.UserService;
import cn.rtangyy.seckilling.utils.CookieUtils;
import cn.rtangyy.seckilling.utils.MD5Util;
import cn.rtangyy.seckilling.utils.UUIDUtil;
import cn.rtangyy.seckilling.vo.LoginVo;
import cn.rtangyy.seckilling.vo.RespBean;
import cn.rtangyy.seckilling.vo.RespBeanEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Service
public class UserServiceImpl implements UserService {

    private static final String COOKIE_NAME_TOKEN = "token";

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {

        String mobile = loginVo.getMobile();
        String frontPwd = loginVo.getPassword();

        User user = getByMobile(mobile);

        if (null == user) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        String dbPwd = user.getPassword();
        String saltDB = user.getSalt();
        String calcPwd = MD5Util.frontPwdToDBPwd(frontPwd, saltDB);
        if (!calcPwd.equals(dbPwd)) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        // 生成随机凭证
        String ticket = UUIDUtil.uuid();
        redisTemplate.opsForValue().set("user:" + ticket, user);
        CookieUtils.setCookie(request, response, "userTicket", ticket);

        return RespBean.success(ticket);
    }


    /**
     * 根据用户手机号码查询用户
     *
     * @param mobile
     * @return
     */
    private User getByMobile(String mobile) {
        return userDao.getByMobile(mobile);

    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)) {
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user != null) {
            CookieUtils.setCookie(request, response, "userTicket", userTicket);
        }
        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {
        User user = getUserByCookie(userTicket, request, response);

        if (user == null) {
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }
        user.setPassword(MD5Util.inputPwd2DBPwd(password, user.getSalt()));
        int result = userDao.updateById(user);
        if (result == 1) {
            // 删除 Redis
            redisTemplate.delete("user:"+userTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);


    }
}
